import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/widget/water_click.dart';

///提示dialog------------------------------------------------------------------------------------------------------------------------
///Title:提示dialog <br/>
///Description: <br/>
///author:xiemiao <br/>
///date: 2020-03-03 <br/>
///version 1.0 <br/>
class TipDialog extends StatelessWidget {
  final String title;
  final String content;
  final String confirmText;

  TipDialog(
    this.content, {
    Key key,
    this.title = "温馨提示",
    this.confirmText = "我知道了",
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Dialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
        backgroundColor: Colors.white,
        child: Material(
          type: MaterialType.transparency,
          child: Container(
            height: ScreenUtil().setHeight(350),
            child: Column(
              children: <Widget>[
                Container(
                  alignment: Alignment.center,
                  padding: Paddings.common,
                  decoration: BoxDecoration(
                    color: CommonColors.primary,
                    borderRadius: BorderRadius.only(topLeft: Radius.circular(8), topRight: Radius.circular(8)),
                  ),
                  child: Text(
                    title,
                    style: TextStyle(color: Colors.white, fontSize: CommonDimens.font_size_large),
                  ),
                ),
                Expanded(
                  child: _centerWidget(),
                ),
                WaterClick(
                  padding: Paddings.common,
                  width: double.infinity,
                  decoration: BoxDecoration(
                    border: Border(
                      top: BorderSide(
                        width: 0.5,
                        color: CommonColors.common_divide_line,
                      ),
                    ),
                  ),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text(
                    confirmText,
                    style: TextStyle(color: CommonColors.primary, fontSize: CommonDimens.font_size_middle),
                  ),
                ),
              ],
            ),
          ),
        ));
  }

  ///中间区域
  Widget _centerWidget() {
    return Container(
      width: double.infinity,
      alignment: Alignment.center,
      padding: Paddings.common,
      color: Colors.white,
      child: Text(
        content,
        maxLines: 3,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(color: CommonColors.second_text, fontSize: CommonDimens.font_size_middle),
      ),
    );
  }
}

///确定,取消dialog------------------------------------------------------------------------------------------------------------------------
///Title:确定,取消dialog <br/>
///Description: <br/>
///author:xiemiao <br/>
///date: 2020-03-03 <br/>
///version 1.0 <br/>
class ConfirmDialog extends StatelessWidget {
  final String content;
  final String title;
  final bool canBackDismiss;
  final String confirmText;
  final String cancelText;
  final VoidCallback confirmCallback;
  final VoidCallback cancelCallback;

  ConfirmDialog(
    this.content, {
    this.title = "温馨提示!",
    this.canBackDismiss = true, //是否能返回取消
    this.confirmText = "确定",
    this.cancelText = "取消",
    this.confirmCallback,
    this.cancelCallback,
  });

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Dialog(
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
            backgroundColor: Colors.white,
            child: Material(
              type: MaterialType.transparency,
              child: Container(
                height: ScreenUtil().setHeight(350),
                child: Column(
                  children: <Widget>[
                    Container(
                      alignment: Alignment.center,
                      padding: Paddings.common,
                      decoration: BoxDecoration(
                        color: CommonColors.primary,
                        borderRadius: BorderRadius.only(topLeft: Radius.circular(8), topRight: Radius.circular(8)),
                      ),
                      child: Text(
                        title,
                        style: TextStyle(color: Colors.white, fontSize: CommonDimens.font_size_large),
                      ),
                    ),
                    Expanded(
                      child: _centerWidget(),
                    ),
                    Container(
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(
                            width: 0.5,
                            color: CommonColors.common_divide_line,
                          ),
                        ),
                      ),
                      child: Row(
                        children: <Widget>[
                          Expanded(
                            child: WaterClick(
                              padding: Paddings.common,
                              decoration: BoxDecoration(
                                border: Border(
                                  right: BorderSide(
                                    width: 0.5,
                                    color: CommonColors.common_divide_line,
                                  ),
                                ),
                              ),
                              onPressed: () {
                                Navigator.pop(context);
                                if (confirmCallback != null) {
                                  confirmCallback();
                                }
                              },
                              child: Text(
                                confirmText,
                                style: TextStyle(color: CommonColors.primary, fontSize: CommonDimens.font_size_middle),
                              ),
                            ),
                          ),
                          Expanded(
                            child: WaterClick(
                              padding: Paddings.common,
                              onPressed: () {
                                Navigator.pop(context);
                                if (cancelCallback != null) {
                                  cancelCallback();
                                }
                              },
                              child: Text(
                                cancelText,
                                style: TextStyle(color: CommonColors.gray, fontSize: CommonDimens.font_size_middle),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            )),
        onWillPop: () => Future.value(canBackDismiss));
  }

  ///中间区域
  Widget _centerWidget() {
    return Container(
      width: double.infinity,
      alignment: Alignment.center,
      padding: Paddings.common,
      color: Colors.white,
      child: Text(
        content,
        maxLines: 3,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(color: CommonColors.second_text, fontSize: CommonDimens.font_size_middle),
      ),
    );
  }
}

///简单输入dialog------------------------------------------------------------------------------------------------------------------------
///Title:简单输入dialog <br/>
///Description: <br/>
///author:xiemiao <br/>
///date: 2020-03-03 <br/>
///version 1.0 <br/>
class InputDialog extends StatelessWidget {
  ///输入类型数组 [0:文本 1:整数 2:小数 3:字母+数字]
  static const List<String> INPUT_TYPE_ARR = ["text", "int", "double", "letterNum"];

  TextEditingController _textEditingController;

  ///内容
  final String content;

  ///标题
  final String title;

  ///是否能返回键取消
  final bool canBackDismiss;

  ///确认文本
  final String confirmText;

  ///取消文本
  final String cancelText;

  ///hint
  final String hintText;

  ///确认回调
  final Function confirmCallback;

  ///取消回调
  final Function cancelCallback;

  ///确认后自动关闭dialog
  final bool autoConfirmClose;

  ///取消后自动关闭dialog
  final bool autoCancelClose;

  ///最大长度
  final int maxLength;

  ///输入类型,见InputDialog.INPUT_TYPE_ARR
  final String inputType;

  ///输入格式
  List<TextInputFormatter> _inputFormatters = [];

  ///键盘类型
  TextInputType keyboardType;

  InputDialog(
    this.content, {
    this.title = "请输入",
    this.canBackDismiss = true, //是否能返回取消
    this.confirmText = "确定",
    this.cancelText = "取消",
    this.confirmCallback,
    this.cancelCallback,
    this.hintText = "请输入",
    this.autoConfirmClose = true, //点击确定自动关闭
    this.autoCancelClose = true, //点击取消自动关闭
    this.maxLength = 200, //最大长度
    this.inputType,
  }) {
    _textEditingController = TextEditingController(text: content);
  }

  @override
  Widget build(BuildContext context) {
    _inputFormatters.clear();
    //根据输入类型设置键盘和输入格式
    if (INPUT_TYPE_ARR[0] == inputType) {
      keyboardType = TextInputType.multiline;
    } else if (INPUT_TYPE_ARR[1] == inputType) {
      keyboardType = TextInputType.number;
      _inputFormatters.add(WhitelistingTextInputFormatter.digitsOnly);
    } else if (INPUT_TYPE_ARR[2] == inputType) {
      keyboardType = TextInputType.numberWithOptions(decimal: true);
      _inputFormatters.add(WhitelistingTextInputFormatter(RegExp("[0-9.]")));
    } else if (INPUT_TYPE_ARR[3] == inputType) {
      keyboardType = TextInputType.emailAddress;
      _inputFormatters.add(WhitelistingTextInputFormatter(RegExp("[0-9a-zA-Z]")));
    } else {
      keyboardType = TextInputType.multiline;
    }

    return WillPopScope(
        child: Dialog(
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
            backgroundColor: Colors.white,
            child: Material(
              type: MaterialType.transparency,
              child: Container(
                height: ScreenUtil().setHeight(400),
                child: Column(
                  children: <Widget>[
                    Container(
                      alignment: Alignment.center,
                      padding: Paddings.common,
                      decoration: BoxDecoration(
                        color: CommonColors.primary,
                        borderRadius: BorderRadius.only(topLeft: Radius.circular(8), topRight: Radius.circular(8)),
                      ),
                      child: Text(
                        title,
                        style: TextStyle(color: Colors.white, fontSize: CommonDimens.font_size_large),
                      ),
                    ),
                    Expanded(
                      child: _centerWidget(),
                    ),
                    Container(
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(
                            width: 0.5,
                            color: CommonColors.common_divide_line,
                          ),
                        ),
                      ),
                      child: Row(
                        children: <Widget>[
                          Expanded(
                            child: WaterClick(
                              padding: Paddings.common,
                              decoration: BoxDecoration(
                                border: Border(
                                  right: BorderSide(
                                    width: 0.5,
                                    color: CommonColors.common_divide_line,
                                  ),
                                ),
                              ),
                              onPressed: () {
                                if (autoConfirmClose) {
                                  Navigator.pop(context);
                                }

                                if (confirmCallback != null) {
                                  confirmCallback(_textEditingController.text);
                                }
                              },
                              child: Text(
                                confirmText,
                                style: TextStyle(color: CommonColors.primary, fontSize: CommonDimens.font_size_middle),
                              ),
                            ),
                          ),
                          Expanded(
                            child: WaterClick(
                              padding: Paddings.common,
                              decoration: BoxDecoration(
                                border: Border(
                                  right: BorderSide(
                                    width: 0.5,
                                    color: CommonColors.common_divide_line,
                                  ),
                                ),
                              ),
                              onPressed: () {
                                _textEditingController.clear();
                              },
                              child: Text(
                                "清除",
                                style: TextStyle(color: CommonColors.red, fontSize: CommonDimens.font_size_middle),
                              ),
                            ),
                          ),
                          Expanded(
                            child: WaterClick(
                              padding: Paddings.common,
                              onPressed: () {
                                if (autoCancelClose) {
                                  Navigator.pop(context);
                                }
                                if (cancelCallback != null) {
                                  cancelCallback();
                                }
                              },
                              child: Text(
                                cancelText,
                                style: TextStyle(color: CommonColors.gray, fontSize: CommonDimens.font_size_middle),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            )),
        onWillPop: () => Future.value(canBackDismiss));
  }

  ///中间区域
  Widget _centerWidget() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.only(left: 2, top: 2, right: 2, bottom: 5),
      margin: EdgeInsets.only(left: 10, top: 10, right: 10, bottom: 10),
      decoration: BoxDecoration(
        color: CommonColors.common_bg,
        borderRadius: BorderRadius.circular(5),
      ),
      child: TextField(
        controller: _textEditingController,
        keyboardType: keyboardType,
        inputFormatters: _inputFormatters,
        maxLines: 10,
        maxLength: maxLength,
        cursorColor: CommonColors.primary,
        style: TextStyle(fontSize: CommonDimens.font_size_middle, color: CommonColors.second_text),
        decoration: InputDecoration(
          border: InputBorder.none,
          hintText: hintText,
          hintStyle: TextStyle(fontSize: CommonDimens.font_size_middle, color: CommonColors.gray),
          contentPadding: EdgeInsets.all(2),
        ),
      ),
    );
  }
}
